package com.acwing.partition5;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/2/10 18:01
 */
public class AC456车站分级 {

    private static final int N = 2010, M = 1000010;
    private static int[] head = new int[N], ver = new int[M], next = new int[M], w = new int[M], din = new int[N];
    private static boolean[] st = new boolean[N];
    private static int[] q = new int[N], dist = new int[N];

    private static int n = 0, m = 0, idx = 0, qh = 0, qt = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(head, -1);
        for (int i = 1; i <= m; i++) {
            ss = reader.readLine().split(" ");
            Arrays.fill(st, false);
            int start = n, end = 1;
            for (int j = 1; j < ss.length; j++) {
                int temp = Integer.parseInt(ss[j]);
                start = Math.min(start, temp);
                end = Math.max(end, temp);
                st[temp] = true;
            }
            int virtual = n + i;
            //不停靠的站点：u->0 边权为0，停靠的站点：u->0 边权为0 0->v 边权为1
            for (int j = start; j <= end; j++) {
                if (st[j]) add(virtual, j, 1);
                else add(j, virtual, 0);
            }
        }
        topsort();
        for (int i = 1; i <= n; i++) dist[i] = 1;
        //按照拓扑序列跑一遍递推公式
        for (int i = 0; i < n + m; i++) {
            int u = q[i];
            for (int j = head[u]; j != -1; j = next[j]) {
                int v = ver[j];
                dist[v] = Math.max(dist[v], dist[u] + w[j]);
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) ans = Math.max(ans, dist[i]);
        writer.write(ans + "\n");
        writer.flush();
    }

    private static void topsort() {
        for (int i = 1; i <= n + m; i++) {
            if (din[i] == 0) q[qt++] = i;
        }
        while (qh != qt) {
            int u = q[qh++];
            if (qh == N) qh = 0;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = ver[i];
                if (--din[v] == 0) {
                    q[qt++] = v;
                    if (qt == N) qt = 0;
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        ver[idx] = b;
        next[idx] = head[a];
        head[a] = idx;
        w[idx++] = c;
        din[b]++;
    }
}
